package com.personal.controller;

import com.personal.controller.result.BackCode;
import com.personal.controller.result.Result;
import com.personal.domain.Industry;
import com.personal.domain.obtain.IndustryDireTree;
import com.personal.service.IndustryService;
import com.personal.util.partDisplay.IndustryPart;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @projectName: smartCommunity_springboot
 * @package: com.personal.controller
 * @className: IndustryController
 * @author: bong~
 * @description: TODO
 * @date: 2023/8/11 13:47
 * @version: 1.0
 */
@RestController
@RequestMapping("/industry")
public class IndustryController {


    @Autowired
    private IndustryService industryService;


    @GetMapping("/All")
    public Result selectAll(){
        List<Industry> list=industryService.selectAll();
        return new Result(list!=null? BackCode.GET_OK:BackCode.GET_ERR,list);
    }

    @GetMapping("")
    public Result selectPartAll(){
        List<IndustryPart> list=industryService.selectPartAll();
        return new Result(list!=null?BackCode.GET_OK:BackCode.GET_ERR,list);
    }

    //点击目录树，获取industryId的list
    @GetMapping("/dictroyTree")
    public Result selectPartByIds(@RequestParam List<Integer> industryIds){
        List<IndustryPart> list=industryService.selectPartByIds(industryIds);
        return new Result(list!=null?BackCode.GET_OK:BackCode.GET_ERR,list);
    }

    @GetMapping("/search")
    public Result selectPartByKey(@RequestParam String name){
        List<IndustryPart> list=industryService.selectPartByKey(name);
        return new Result(list!=null?BackCode.GET_OK:BackCode.GET_ERR,list);
    }

    @PostMapping()
    public Result add(@RequestBody Industry industry){
        boolean flag=industryService.add(industry);
        return new Result(flag?BackCode.GET_OK:BackCode.GET_ERR,flag);
    }


    @GetMapping("/updateData")
    public Result getUpdateData(@RequestParam("industryId") int industryId){
        Industry industry=industryService.getUpdateData(industryId);
        return new Result(industry!=null?BackCode.GET_OK:BackCode.GET_ERR,industry);
    }
    @PutMapping("")
    public Result update(@RequestBody Industry industry){
        boolean flag=industryService.update(industry);
        return new Result(flag?BackCode.UPDATE_OK:BackCode.UPDATE_ERR,flag);
    }



    @GetMapping("/directoryTree")
    public Result getDirectoryTree(){
        List<IndustryDireTree> list=industryService.getDirectory(0);
        return new Result(list!=null?BackCode.GET_OK:BackCode.GET_ERR,list);

    }
}
